package user

import (
	"bytes"
	"chaowan/data_source"
	"chaowan/models"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"time"
)

func Zhuce(userinfo models.User) (data T5) {

	user_id := UserId{
		WechatOpenid: userinfo.RoutineOpenid,
		WechatAppId:  appID,
	}
	trace := Trace{
		ClickId: userinfo.ClickId,
	}

	action := Actions{
		ActionTime: time.Now().Unix(),
		UserId:     user_id,
		ActionType: "REGISTER",
		Trace:      trace,
	}
	var actions []Actions
	actions = append(actions, action)
	requests := RequestBody{
		Actions: actions,
	}

	marshal2, _ := json.Marshal(requests)
	body, _ := json.Marshal(requests)
	client := &http.Client{}
	req, err := http.NewRequest("POST", "http://tracking.e.qq.com/conv", bytes.NewBuffer(body))
	if err != nil {
		panic(err)
	}
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("cache-control", "no-cache")
	resp, err := client.Do(req)
	defer resp.Body.Close()
	body1, err := ioutil.ReadAll(resp.Body)
	var dylogs []models.Guanggao
	dylog2 := models.Guanggao{
		Addtime: time.Now().Unix(),
		Uid:     userinfo.ID,
		Title:   "提交" + string(marshal2),
	}
	dylog1 := models.Guanggao{
		Addtime: time.Now().Unix(),
		Uid:     userinfo.ID,
		Title:   "返回" + string(body1),
	}
	dylogs = append(dylogs, dylog2)
	dylogs = append(dylogs, dylog1)
	data_source.Db.Create(&dylogs)
	if err != nil {
		fmt.Println(err)
	}
	r := T5{}
	err = json.Unmarshal(body1, &r)

	if err != nil {
		fmt.Println(err)
	}
	return r
}
func Payshangbao(userinfo models.User, price int) (data T5) {

	user_id := UserId{
		WechatOpenid: userinfo.RoutineOpenid,
		WechatAppId:  appID,
	}
	trace := Trace{
		ClickId: userinfo.ClickId,
	}
	actionParam := ActionParam{
		Value: price,
	}
	action := Actions{
		ActionTime:  time.Now().Unix(),
		UserId:      user_id,
		ActionType:  "COMPLETE_ORDER",
		Trace:       trace,
		ActionParam: actionParam,
	}
	var actions []Actions
	actions = append(actions, action)
	requests := RequestBody{
		Actions: actions,
	}

	marshal2, _ := json.Marshal(requests)
	body, _ := json.Marshal(requests)
	client := &http.Client{}
	req, err := http.NewRequest("POST", "http://tracking.e.qq.com/conv", bytes.NewBuffer(body))
	if err != nil {
		panic(err)
	}
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("cache-control", "no-cache")
	resp, err := client.Do(req)
	defer resp.Body.Close()
	body1, err := ioutil.ReadAll(resp.Body)
	var dylogs []models.Guanggao
	dylog2 := models.Guanggao{
		Addtime: time.Now().Unix(),
		Uid:     userinfo.ID,
		Title:   "提交" + string(marshal2),
	}
	dylog1 := models.Guanggao{
		Addtime: time.Now().Unix(),
		Uid:     userinfo.ID,
		Title:   "返回" + string(body1),
	}
	dylogs = append(dylogs, dylog2)
	dylogs = append(dylogs, dylog1)
	data_source.Db.Create(&dylogs)
	if err != nil {
		fmt.Println(err)
	}
	r := T5{}
	err = json.Unmarshal(body1, &r)

	if err != nil {
		fmt.Println(err)
	}
	return r
}

type RequestBody struct {
	Actions []Actions `json:"actions"`
}
type Actions struct {
	ActionTime  int64       `json:"action_time"`
	UserId      UserId      `json:"user_id"`
	ActionType  string      `json:"action_type"`
	Trace       Trace       `json:"trace"`
	ActionParam ActionParam `json:"action_param"`
}
type UserId struct {
	WechatOpenid string `json:"wechat_openid"`
	WechatAppId  string `json:"wechat_app_id"`
}
type Trace struct {
	ClickId string `json:"click_id"`
}
type ActionParam struct {
	Value int `json:"value"`
}
type T5 struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}
